/*
 * ProcessingJobsPanel.java
 *
 * Created on October 12, 2008, 3:31 PM
 */
package botoseis.mainGui.workflows;

import java.awt.event.ActionEvent;
import java.util.TimerTask;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Timer;
import javax.swing.table.DefaultTableModel;
import botoseis.mainGui.utils.Utils;
import botoseis.mainGui.workflows.WorkflowJob;
import botoseis.mainGui.utils.RendererTable;

public class ProcessingJobsPanel extends javax.swing.JPanel {

    /** Creates new form ProcessingJobsPanel */
    public ProcessingJobsPanel() {
        initComponents();
        Action action = new AbstractAction("Update jobs") {

            public void actionPerformed(ActionEvent e) {
                updateJobsTable();
            }
        };
        Timer timer = new Timer(3000, action);
        timer.start();
        jobsTable.setDefaultRenderer(Object.class, new RendererTable());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jobsTable = new javax.swing.JTable();
        jToolBar1 = new javax.swing.JToolBar();
        btnView = new javax.swing.JButton();
        btnRun = new javax.swing.JButton();
        btnStop = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        btnRemove = new javax.swing.JButton();

        jobsTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null}
            },
            new String [] {
                "Title", "Description", "Status", "Start", "End", "Duration", "Processed(MB)", ""
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, true, false, false, false, false, false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jobsTable);
        jobsTable.getColumnModel().getColumn(7).setMinWidth(0);
        jobsTable.getColumnModel().getColumn(7).setPreferredWidth(0);
        jobsTable.getColumnModel().getColumn(7).setMaxWidth(0);

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);

        btnView.setIcon(new javax.swing.ImageIcon(getClass().getResource("/botoseis/pics/Information16.png"))); // NOI18N
        btnView.setToolTipText("Show output");
        btnView.setFocusable(false);
        btnView.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnView.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnView.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnViewActionPerformed(evt);
            }
        });
        jToolBar1.add(btnView);

        btnRun.setIcon(new javax.swing.ImageIcon(getClass().getResource("/botoseis/pics/Play16.png"))); // NOI18N
        btnRun.setToolTipText("Execute selected job");
        btnRun.setFocusable(false);
        btnRun.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnRun.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnRun.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRunActionPerformed(evt);
            }
        });
        jToolBar1.add(btnRun);

        btnStop.setIcon(new javax.swing.ImageIcon(getClass().getResource("/botoseis/pics/Stop16.png"))); // NOI18N
        btnStop.setToolTipText("Stop select job");
        btnStop.setFocusable(false);
        btnStop.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnStop.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnStop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStopActionPerformed(evt);
            }
        });
        jToolBar1.add(btnStop);
        jToolBar1.add(jSeparator1);

        btnRemove.setIcon(new javax.swing.ImageIcon(getClass().getResource("/botoseis/pics/Remove16.png"))); // NOI18N
        btnRemove.setToolTipText("Remove selected item from table");
        btnRemove.setFocusable(false);
        btnRemove.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnRemove.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemoveActionPerformed(evt);
            }
        });
        jToolBar1.add(btnRemove);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jToolBar1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 568, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 568, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    public void addJob(WorkflowJob njob) {
        m_jobsList.add(njob);
        updateJobsTable();
    }

    private void updateJobsTable() {
        for (int i = 0; i < MAX_ROWS; i++) {
            jobsTable.setValueAt("", i, 0);
            jobsTable.setValueAt("", i, 1);
            jobsTable.setValueAt("", i, 2);
            jobsTable.setValueAt("", i, 3);
        }

        String title;
        for (int i = 0; i < m_jobsList.size(); i++) {
            title = m_jobsList.get(i).getTitle();
            String user = System.getProperty("user.name");
            String path = m_jobsList.get(i).getHomeDir();

            jobsTable.setValueAt(title, i, 0);
            jobsTable.setValueAt(user + " - " + path, i, 1);
            jobsTable.setValueAt(m_jobsList.get(i).getStatus(), i, 2);
            jobsTable.setValueAt(Utils.getDateFormated(m_jobsList.get(i).getTimeStart()), i, 3);
            jobsTable.setValueAt(Utils.getDateFormated(m_jobsList.get(i).getTimeStop()), i, 4);
            jobsTable.setValueAt(m_jobsList.get(i).getDuration(), i, 5);
            jobsTable.setValueAt(m_jobsList.get(i).getProcessed(), i, 6);
            jobsTable.setValueAt(i, i, 7);
        }
    }

private void btnViewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnViewActionPerformed
    int row = jobsTable.getSelectedRow();
    if (row >= 0) {
        m_jobsList.get(row).getFlowLog().setVisible(true);
//        java.io.File logf = m_jobsList.get(row).getLogfile();
//        WorkflowLogDlg dlg = new WorkflowLogDlg(null, false);
//        dlg.loadFile(logf);
//
//        dlg.setVisible(true);

    }

}//GEN-LAST:event_btnViewActionPerformed

private void btnRunActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRunActionPerformed
    int row = jobsTable.getSelectedRow();
    if (row >= 0) {
        m_jobsList.get(row).getFlowLog().loadFromFile(null);
        m_jobsList.get(row).start();
    }


}//GEN-LAST:event_btnRunActionPerformed

private void btnRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemoveActionPerformed
    int row = jobsTable.getSelectedRow();
    if (row >= 0) {
        if (!jobsTable.getValueAt(row, 7).toString().equals("")) {
            java.io.File hd = new java.io.File(m_jobsList.get((Integer.parseInt(jobsTable.getValueAt(row, 7).toString()))).getHomeDir());

            int ans = javax.swing.JOptionPane.showConfirmDialog(this, "The folder :" + hd.toString()
                    + "\nwill be permanently removed!", "User folder removal", javax.swing.JOptionPane.OK_CANCEL_OPTION);
            if (ans == javax.swing.JOptionPane.OK_OPTION) {
                Utils.deleteFile(hd);
                int id = Integer.parseInt(jobsTable.getValueAt(row, 7).toString());
                for (int i = 0; i < jobsTable.getColumnCount(); i++) {
                    jobsTable.setValueAt("", row, i);
                }
                m_jobsList.remove(id);
                jobsTable.updateUI();

            }
        }

    }

}//GEN-LAST:event_btnRemoveActionPerformed

private void btnStopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStopActionPerformed
    int row = jobsTable.getSelectedRow();
    if (row >= 0) {
        m_jobsList.get(new Integer(jobsTable.getValueAt(row, 7).toString())).stop();
    }

}//GEN-LAST:event_btnStopActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnRemove;
    private javax.swing.JButton btnRun;
    private javax.swing.JButton btnStop;
    private javax.swing.JButton btnView;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JTable jobsTable;
    // End of variables declaration//GEN-END:variables
    private java.util.Vector<WorkflowJob> m_jobsList = new java.util.Vector<WorkflowJob>();
    private int MAX_ROWS = 50;
}
